package com.zhengjiang.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author zhengjiang
 * @version 1.0.0
 * @description 有返回值的线程
 * @date 2021/6/13
 **/
public class CallableTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable callable = ()-> {
            int i = 0;
            for (;i<100;i++){
                Thread.sleep(100);
                System.out.println(Thread.currentThread().getName() + "第" + i + "次循环");
            }
            return i;
        };

        FutureTask futureTask = new FutureTask(callable);
        new Thread(futureTask).start();
        System.out.println("Callable线程返回值：" + futureTask.get());
        System.out.println("主线程结束");

    }
}
